package com.sotnyk.sspeed.visual;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.NumberPicker;

import com.sotnyk.sspeed.R;

public class SpeedLimitDialogFragment extends DialogFragment {

	public static final String LIMIT_NUMBER_PARAM = "LIMIT_NUMBER_PARAM";
	public static final String SPEED_LIMIT_PARAM = "SPEED_LIMIT_PARAM";

	public interface SpeedLimitDialogListener {
		public void onDialogPositiveClick(SpeedLimitDialogFragment dialog);
	}

    private SpeedLimitDialogListener mListener;
    private int mLimitNumber;
	private int mSpeedLimit;

	/*
	 * public SpeedLimitDialogFragment(int limitNumber, int currentLimit){
	 * super(); mLimitNumber = limitNumber; mSpeedLimit = currentLimit; }
	 */

	public int getLimitNumber() {
		return mLimitNumber;
	}

	public int getSpeedLimit() {
		return mSpeedLimit;
	}

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {

		Bundle args = getArguments();
		if (args != null) {
			mLimitNumber = args.getInt(LIMIT_NUMBER_PARAM, 0);
			mSpeedLimit = args.getInt(SPEED_LIMIT_PARAM, 0);
		}

		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		// Get the layout inflater
        //LayoutInflater inflater = getActivity().getLayoutInflater();
        View v = View.inflate(getActivity(), R.layout.dialog_speed_limits, null);

        NumberPicker speedLimitNumberPicker = (NumberPicker) v
                .findViewById(R.id.speedLimit_numberPicker);
        speedLimitNumberPicker.setMinValue(0);
        speedLimitNumberPicker.setMaxValue(200);
        speedLimitNumberPicker.setValue(mSpeedLimit);
        speedLimitNumberPicker
                .setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {

                    @Override
                    public void onValueChange(NumberPicker picker, int oldVal,
                                              int newVal) {
                        mSpeedLimit = newVal;
                        Bundle args = getArguments();
                        args.putInt(SPEED_LIMIT_PARAM, newVal);
                    }
                });

		// Inflate and set the layout for the dialog
		// Pass null as the parent view because its going in the dialog layout
		builder.setView(v)
				// Add action buttons
				.setPositiveButton(R.string.set_button_text,
						new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int id) {
								if (mListener != null) {
									mListener
											.onDialogPositiveClick(SpeedLimitDialogFragment.this);
								}
							}
						})
				.setNegativeButton(R.string.cancel_button_text,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
							}
						});
		builder.setTitle(R.string.pr_speed_limit_dialog_title);
		return builder.create();
	}

	public void setListener(SpeedLimitDialogListener listener) {
		mListener = listener;
	}
}
